package com.neborosoft.lib.collections;

import java.util.Iterator;
import java.util.regex.Matcher;

/**
 * Project: FlyingDog
 * User: stikhonenko
 * Date: 2/6/13
 * Time: 7:42 PM
 */
public class MatcherIterator implements Iterator<String>{
    private Matcher matcher;
    private int groupIndex = -1;

    public MatcherIterator(Matcher matcher, int groupIndex) {
        this.matcher = matcher;
        this.groupIndex = groupIndex;
    }

    public MatcherIterator(Matcher matcher) {
        this.matcher = matcher;
    }

    public int getGroupIndex() {
        return groupIndex;
    }

    public void setGroupIndex(int groupIndex) {
        this.groupIndex = groupIndex;
    }

    public Matcher getMatcher() {
        return matcher;
    }

    public void setMatcher(Matcher matcher) {
        this.matcher = matcher;
    }

    @Override
    public boolean hasNext() {
        return matcher.find();
    }

    @Override
    public String next() {
        if (groupIndex >= 0) {
            return matcher.group(groupIndex);
        } else {
            return matcher.group();
        }
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }
}
